package com.test.prolenders.activity_prolenders.ln_prolenders;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.test.prolenders.R;
import com.test.prolenders.base_prolenders.BaseActivity_prolenders;
import com.test.prolenders.bean_prolenders.userinfo_prolenders.LnAplyDetailInfo_prolenders;
import com.test.prolenders.bean_prolenders.userinfo_prolenders.LnAplyItem_prolenders;
import com.test.prolenders.dialog_prolenders.CustomerServiceDialog_prolenders;
import com.test.prolenders.util_prolenders.http_prolenders.HttpUtil_prolenders;

import java.util.ArrayList;

public class LnAplyActivity_prolenders extends BaseActivity_prolenders {

    private TextView currencyTopTv,amountTopTv,termTopTv;
    private LinearLayout pGroupLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ln_aply_prolenders);
        page = "APPLY";

        SwipeRefreshLayout refreshLayout = findViewById(R.id.ala_refreshview);
        refreshLayout.setOnRefreshListener(() -> {
            refreshLayout.setRefreshing(false);
            HttpUtil_prolenders.getInstance().requestLnAplyDetail(this,this::onAplyDetailResp);
        });

        findViewById(R.id.ala_ib_back).setOnClickListener(v -> onBackPressed());
        findViewById(R.id.ala_ib_customerservice).setOnClickListener(v -> new CustomerServiceDialog_prolenders(this).show());

        currencyTopTv = findViewById(R.id.ala_tv_currencytop);
        amountTopTv = findViewById(R.id.ala_tv_amounttop);
        termTopTv = findViewById(R.id.ala_tv_termtop);
        pGroupLayout = findViewById(R.id.ala_layout_param_group);


        findViewById(R.id.ala_btn_submit).setOnClickListener(v -> {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page ,"SUBMIT_CLICK","");
            HttpUtil_prolenders.getInstance().aply(this,this::onLnAplyResp);
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        HttpUtil_prolenders.getInstance().requestLnAplyDetail(this,this::onAplyDetailResp);
    }

    @SuppressLint("SetTextI18n")
    public void onAplyDetailResp(){

        String amount = LnAplyDetailInfo_prolenders.getInstance().getAmount();
        String currency = LnAplyDetailInfo_prolenders.getInstance().getCurrency();

        amountTopTv.setText(amount);
        currencyTopTv.setText(currency);
        termTopTv.setText(LnAplyDetailInfo_prolenders.getInstance().getTermStr(this));

        refreshParamView();
    }

    @SuppressLint("InflateParams")
    private void refreshParamView(){

        pGroupLayout.removeAllViews();

        ArrayList<LnAplyItem_prolenders> params = LnAplyDetailInfo_prolenders.getInstance().getDetailItemList();

        for (int i=0;i<params.size();i++){

            LnAplyItem_prolenders param = params.get(i);

            if (param == null) continue;

            View view;
            TextView pNameTv;
            TextView pValueTv;

            if (param.isTitle()){
                if (i>0) pGroupLayout.addView(LayoutInflater.from(this).inflate(R.layout.item_lnaply_divider_prolenders,null));
                view = LayoutInflater.from(this).inflate(R.layout.item_lnaply_title_prolenders,null);
                pNameTv = view.findViewById(R.id.ilt_tv_p_name);
                pValueTv = view.findViewById(R.id.ilt_tv_p_value);
            }else {
                view = LayoutInflater.from(this).inflate(R.layout.item_lnaply_detail_prolenders,null);
                pNameTv = view.findViewById(R.id.ild_tv_p_name);
                pValueTv = view.findViewById(R.id.ild_tv_p_value);
            }

            pNameTv.setText(param.getShowName());
            pValueTv.setText(param.getShowDetail());

            pGroupLayout.addView(view);

        }

    }

    public void onLnAplyResp(){
        HttpUtil_prolenders.getInstance().userProcess(this,this::onProcessResp);
    }

    public void onProcessResp(){
        Intent intent = new Intent(this, MainActivity_prolenders.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }

}